package com.sgb.kjwl.viewmodel.machine;

import android.content.Context;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AskLeaseFactoryEntity;
import com.sgb.kjwl.model.entity.AskLeaseFarmEntity;
import com.sgb.kjwl.model.entity.AskLeaseLandEntity;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.widget.lease.common.AskLeaseReleaseView;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseLayoutModel {
    private Context context;
    private AskLeaseReleaseView askLeaseReleaseView;

    public AskLeaseLayoutModel(Context context, AskLeaseReleaseView askLeaseReleaseView) {
        this.context = context;
        this.askLeaseReleaseView = askLeaseReleaseView;
    }

    //求租类型
    public List<DictEntity3> getType() {
        List<DictEntity3> typeList = new ArrayList<>();
        typeList.add(new DictEntity3("0", "机械求租"));
        typeList.add(new DictEntity3("1", "设备求租"));
        typeList.add(new DictEntity3("2", "车辆求租"));
        typeList.add(new DictEntity3("3", "房产求租"));
        typeList.add(new DictEntity3("4", "土地求租"));
        typeList.add(new DictEntity3("5", "工厂求租"));
        typeList.add(new DictEntity3("6", "农机求租"));
        return typeList;
    }
    //获取汽车类型分类信息
    public void getModelSelect() {
        DialogHelper.showProgressDialog(context, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getModelSelect().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showCarTypeView(response.body().getData());
                } else {
                    MToast.showToast(context, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取类型分类数据
    public void getTypeModel() {
        DialogHelper.showProgressDialog(context, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getAllTypeList().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showTypeView(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //获取省市区
    public void getAddressModel() {
        DialogHelper.showProgressDialog(context, "加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.initAddressData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }


    // 获取付款方式
    public void getAllPayWay() {
        DialogHelper.showProgressDialog(context, "加载中", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getAllPayWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showAllPayWay(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "服务异常");
            }
        });
    }

    //供水供电-土地
    public void getPowerWayLand() {
        APIManager.getInstance().getMachineAPI().getPowerWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showPowerWay(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    //供水供电-工厂
    public void getPowerWayFactory() {
        APIManager.getInstance().getMachineAPI().getSupplyConfig().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showPowerWay(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取机械作业方式
    public void getMechanicalWork() {
        APIManager.getInstance().getMachineAPI().getMechanicalWork().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showMechanicalWork(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 日照情况
    public void getSunshineSupply() {
        APIManager.getInstance().getMachineAPI().getSunshineSupply().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showSunshineSupply(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取办公区域、停车区域、住宿区域、卸货平台配置信息
    public void getHasNotConfig() {
        APIManager.getInstance().getMachineAPI().getHasNotConfig().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseReleaseView.showPurposeList(response.body().getData());
                    } else {
                        MToast.showToast(context, "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    //  机械，设置，车辆，房产发布求租信息
    public void addOrupdate(PublishLeaseDataEntity publishleasedataentity) {
        DialogHelper.showProgressDialog(context, null, "提交数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getequimentLeasing(publishleasedataentity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(context, "发布失败..");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showSuccess();
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "发布失败..");
            }
        });
    }

    //  土地发布求租信息
    public void addOrupdate(AskLeaseLandEntity askLeaseLandEntity) {
        DialogHelper.showProgressDialog(context, "提交中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().addOrUpdate(askLeaseLandEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(context, "发布失败");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showSuccess();
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "发布失败...");
            }
        });
    }

    //  工厂发布求租信息
    public void addOrupdate(AskLeaseFactoryEntity askLeaseFactoryEntity) {
        DialogHelper.showProgressDialog(context, "提交中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().addOrUpdateFactory(askLeaseFactoryEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(context, "发布失败");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showSuccess();
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "发布失败...");
            }
        });
    }

    //  农机求租发布
    public void addOrupdate(AskLeaseFarmEntity askLeaseFarmEntity) {
        DialogHelper.showProgressDialog(context, "提交中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().addOrUpdateFarm(askLeaseFarmEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(context, "发布失败");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showSuccess();
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "发布失败...");
            }
        });
    }

    //  获取机械价格单位
    public void getMachineUnit() {
        DialogHelper.showProgressDialog(context, "数据加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // 获取设备价格单位
    public void getRentpriceUnit() {
        DialogHelper.showProgressDialog(context, "数据加载中...").setCanceledOnTouchOutside(true);
        APIManager.getInstance().getMachineAPI().getRentpriceUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //  车辆价格单位选择
    public void getNeedLeaseCarPriceUnit() {
        DialogHelper.showProgressDialog(context, "数据加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getNeedLeaseCarPrice().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //房产价格单位选择
    public void getEstatePriceHouseUnit() {
        DialogHelper.showProgressDialog(context, "数据加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getEstatePriceUnitNew().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取土地价格单位
     */
    public void getPriceUnitLandModel() {
        DialogHelper.showProgressDialog(context, "加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPriceUnitLand().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取价格单位
     */
    public void getPriceUnitSelectFactoryModel() {
        DialogHelper.showProgressDialog(context, "加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPriceUnitSelectFactory().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取农机价格单位
     */
    public void getPriceUnitSelectFarmModel() {
        DialogHelper.showProgressDialog(context, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPriceUnitSelectFarm().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.setPriceUnit(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 租赁时长单位
     * @param type 模块类型，默认0机械
     */
    public void getLeaseTimeUnit(String type){
        Map map= new HashMap();
        map.put("pageSize",20);
        map.put("pageNum",1);
        map.put("type",type);

        APIManager.getInstance().getLeaseAPI().getLeaseTimeUnit(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseReleaseView.showLeaseTime(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
            }
        });
    }
}
